package interview;

import java.util.Comparator;
import java.util.PriorityQueue;

public class interview_17_14 {
    public int[] smallestK(int[] arr, int k) {
        PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        if(k==0){
            return new int[0];
        }
        for(int i=0;i<arr.length;++i){
            if(queue.size()<k){
                queue.add(arr[i]);
            }else if(queue.peek()<=arr[i]){
                continue;
            }else{
                queue.add(arr[i]);
                queue.poll();
            }
        }
        int[] result=new int[queue.size()];
        int size= queue.size();
        for(int i=0;i<size;++i){
            result[i]=queue.poll();
        }
        return result;
    }
}
